/**
 * @author casazaft
 */
Package("xclab.mvc.core").Class("Controller", {
        Controller: function(){
 this.Super.constructor();            this.commandMap = {};
            this.initializeController();
        },
        Methods: {
            initializeController: function(){
                var View = Import("xclab.mvc.core.View");
                this.view = View.getInstance();
                
            },
            executeCommand: function(note){
            
                var commandClassRef = this.commandMap[note.getName()];
                
                if (!commandClassRef) 
                    return;
                var commandInstance = new commandClassRef();
                commandInstance.execute(note);
            },
            registerCommand: function(noteName, commandClassRef){
                var Observer = Import("xclab.mvc.patterns.observer.Observer");
                if (!this.commandMap[noteName]) {
                    this.view.registerObserver(noteName, new Observer(this.executeCommand, this));
                }
                this.commandMap[noteName] = commandClassRef;
            },
            hasCommand: function(noteName){
                return this.commandMap.hasOwnProperty(noteName);
            },
            removeCommand: function(noteName){
                if (this.hasCommand(noteName)) {
                    this.view.removeObserver(noteName, this);
                    delete this.commandMap[noteName];
                }
            }
        },
        Extends: j2j.patterns.Singleton
    })
